using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using Grr;
using Grr.Web;
using Grr.Models;

using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Views.Aspx;

public partial class Views_layouts_default_w_sidebar : MasterPage, ITabContainer
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MembershipUser user = Membership.GetUser();
            if (user != null)
            {
                LoggedInUserName = user.UserName;
                OnUserLoggedIn(user, true);
            }
        }
    }

    public string LoggedInUserName
    {
        get {
            object o = ViewState["LoggedInUserName"];
            if (o == null)
                return String.Empty;
            return (string)o;
        }
        set {
            ViewState["LoggedInUserName"] = value;
        }
    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        OpenLoginWindow();
    }
    protected void CancelLoginLinkButton_Click(object sender, EventArgs e)
    {
        CloseLoginWindow();
    }
    protected void SignUpButton_Click(object sender, EventArgs e)
    {
        OpenRegisterWindow();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        CloseLoginWindow();
        OpenRegisterWindow();
    }
    protected void CancelRegisterButton_Click(object sender, EventArgs e)
    {
        CloseRegisterWindow();
    }

    private void OpenLoginWindow()
    {
        LoginUserNameTextBox.Text = String.Empty;
        LoginPasswordTextBox.Text = String.Empty;

        LoginClearMessages();

        login_window.Visible = true;
    }

    private void CloseLoginWindow()
    {
        login_window.Visible = false;
    }

    private void OpenRegisterWindow()
    {
        RegisterUserNameTextBox.Text = String.Empty;
        RegisterEmailTextBox.Text = String.Empty;
        RegisterPasswordTextBox.Text = String.Empty;
        RegisterConfirmPasswordTextbox.Text = String.Empty;
        RegisterLocationTextBox.Text = String.Empty;

        RegisterClearMessages();

        register_window.Visible = true;
    }

    private void CloseRegisterWindow()
    {
        register_window.Visible = false;
    }

    public void SetCurrentTab(string tab)
    {
        WebControl ctl = FindControl(String.Concat(tab, "_tab")) as WebControl;

        if (ctl != null)
            ctl.CssClass = "current";
    }


    protected void LoginSubmitButton_Click(object sender, EventArgs e)
    {
        string tainted_username = LoginUserNameTextBox.Text.Trim ();
        string tainted_password = LoginPasswordTextBox.Text.Trim ();

        LoginClearMessages ();

        if (!Validator.IsValidUserName(tainted_username))
        {
            LoginInvalidUserNameImage.Visible = true;
            LoginErrorLabel.Text = "Invalid user name.";
            LoginErrorLabel.Visible = true;
            return;
        }

        if (!Validator.IsValidPassword(tainted_password))
        {
            LoginInvalidPasswordImage.Visible = true;
            LoginErrorLabel.Text = "Invalid password.";
            LoginErrorLabel.Visible = true;
            return;
        }

        if (!Membership.ValidateUser(tainted_username, tainted_password))
        {
            LoginErrorLabel.Text = "Username and password do not match.";
            LoginErrorLabel.Visible = true;
            return;
        }

        // Username and password aren't tainted if they've made it through
        // all the invalidation above.
        LoginUser(tainted_username, tainted_password);
    }

    private void LoginUser(string username, string password)
    {
        FormsAuthentication.SetAuthCookie(username, false);
        FormsAuthentication.Authenticate(username, password);

        LoggedInUserName = username;
        OnUserLoggedIn(Membership.GetUser (username), false);
        CloseLoginWindow();
    }

    private void OnUserLoggedIn(MembershipUser user, bool page_load)
    {
        HUDPanel.ActiveViewIndex = 1;
        HUDPanel.ForceAnUpdate();

        if (!page_load)
        {
            GrurrahViewPage view_page = (GrurrahViewPage)Page;
            view_page.OnUserLoggedIn (user);
        }
    }

    private void OnUserLoggedOut()
    {
        HUDPanel.ActiveViewIndex = 0;
        HUDPanel.ForceAnUpdate();

        GrurrahViewPage view_page = (GrurrahViewPage)Page;
        view_page.OnUserLoggedOut ();
    }

    private void LoginClearMessages()
    {
        LoginInvalidUserNameImage.Visible = false;
        LoginInvalidPasswordImage.Visible = false;
        LoginErrorLabel.Visible = false;
    }

    private void RegisterClearMessages()
    {
        RegisterUserNameErrorImage.Visible = false;
        RegisterEmailErrorImage.Visible = false;
        RegisterPasswordErrorImage.Visible = false;
        RegisterCofirmPasswordErrorImage.Visible = false;
        RegisterLocationErrorImage.Visible = false;
        RegisterErrorLabel.Visible = false;
    }
    protected void RegisterSubmitButton_Click(object sender, EventArgs e)
    {
        string tainted_username = RegisterUserNameTextBox.Text.Trim ();
        string tainted_email = RegisterEmailTextBox.Text.Trim ();
        string tainted_password = RegisterPasswordTextBox.Text.Trim ();
        string tainted_confirm_password = RegisterConfirmPasswordTextbox.Text.Trim ();
        string tainted_location = RegisterLocationTextBox.Text.Trim();

        RegisterClearMessages();

        if (!Validator.IsValidUserName(tainted_username))
        {
            RegisterUserNameErrorImage.Visible = true;
            RegisterErrorLabel.Text = "Invalid UserName.";
            RegisterErrorLabel.Visible = true;
            return;
        }

        if (Membership.GetUser(tainted_username) != null)
        {
            RegisterUserNameErrorImage.Visible = true;
            RegisterErrorLabel.Text = "Sorry, that username is not available.";
            RegisterErrorLabel.Visible = true;
            return;
        }

        if (!Validator.IsValidEmail(tainted_email))
        {
            RegisterEmailErrorImage.Visible = true;
            RegisterErrorLabel.Text = "Invalid email address.";
            RegisterErrorLabel.Visible = true;
            return;
        }

        if (!Validator.IsValidPassword(tainted_password))
        {
            RegisterPasswordErrorImage.Visible = true;
            RegisterErrorLabel.Text = "Invalid password.";
            RegisterErrorLabel.Visible = true;
            return;
        }

        if (tainted_password != tainted_confirm_password)
        {
            RegisterCofirmPasswordErrorImage.Visible = true;
            RegisterErrorLabel.Text = "Password confirm does not match password.";
            RegisterErrorLabel.Visible = true;
            return;
        }

        if (!Validator.IsValidLocation (tainted_location)) {
            RegisterLocationErrorImage.Visible = true;
            RegisterErrorLabel.Text = "Location is invalid.";
            RegisterErrorLabel.Visible = true;
            return;
        }

        // Username and password are valid if they've made it through
        // all the validation above.
        string error;
        if (!People.CreateUser(tainted_username, tainted_email, tainted_password, tainted_location, out error))
        {
            RegisterErrorLabel.Text = error;
            RegisterErrorLabel.Visible = true;
            return;
        }
        
        LoginUser(tainted_username, tainted_password);
        CloseRegisterWindow();
    }
    protected void SignOutButton_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();

        LoggedInUserName = null;
        OnUserLoggedOut();
    }
}
